/*
 * @Author: wangming
 * @Date: 2021-05-07 14:03:01
 * @LastEditors: wangming
 * @LastEditTime: 2022-05-18 17:14:29
 * @Description: 开源组件二次封装拓展
 */
import { Input } from "element-ui";
export default {
  components: { eInput: Input },
  props: {
    value: {
      type: String,
      default: "",
    },
    isNumber: {
      type: Boolean,
      default: false,
    },
    limitNumber: {
      type: [String, Number],
      default: "0",
    },
  },
  methods: {
    async select() {
      await this.$nextTick();
      this.$refs.input.select();
    },
    async blur() {
      await this.$nextTick();
      this.$refs.input.blur();
    },
    async focus() {
      await this.$nextTick();
      this.$refs.input.focus();
    },
    Nformatter(v = "") {
      v += "";
      let limitStr = "";
      if (/^0\d/.test(v)) {
        v = v.replace(/^0/, "");
      }
      let num = v.replace(/[^0-9.]/g, "");
      if (this.limitNumber == 0) {
        num = v.replace(/[^0-9]/g, "");
      }
      var arr = num.split(".");
      var fixed = arr[1];
      var firstNum = arr[0];
      if (fixed != undefined) {
        let str = fixed.substring(0, this.limitNumber);
        limitStr = firstNum + "." + str;
      } else {
        limitStr = firstNum;
      }
      return limitStr.replace(/^\./, "");
    },
  },
  render() {
    const listeners = Object.assign({}, this.$listeners, {
      blur: (e) => {
        if (!!this.value) {
          let str = this.value.replace(/\s*$/, "");
          if (this.isNumber) str = str.replace(/\.$/, "");
          if (this.value !== str) {
            this.$emit("input", str);
          }
        }
        this.$emit("blur");
      },
      input: (e) => {
        if (this.isNumber) e = this.Nformatter(e);
        this.$emit("input", e.replace(/^\s*/, ""));
      },
    });
    return (
      <e-input
        value={this.value}
        on={listeners}
        attrs={this.$attrs}
        ref="input"
      >
        {Object.keys(this.$slots).map((name) => (
          <template slot={name}>{this.$slots[name]}</template>
        ))}
      </e-input>
    );
  },
};
